<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="/src/api.js"></script>
    <title>登录</title>
    <style>
        .title {
            text-align: left;
            font-family: "Microsoft YaHei";
            color: #409EFF;
            text-indent: 24px;
        }
        #app {
            max-width: 480px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1 class="title">登录</h1>
        <el-result icon="success" title="登录成功!" v-if="success"></el-result>
        <el-form label-width="80px" :model="form" width="100%">
            <el-form-item label="用户名">
                <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model="form.password" type="password" placeholder="请输入密码" @keyup.enter.native="login"></el-input>
            </el-form-item>
            <div style="width: 50%; margin: 0 auto;transform: translateX(40px)" >
                <el-button style="width: 100%" type="primary" @click="login">登录</el-button>
                <el-link type="primary" href="register.html" style="margin-left: 44%;margin-top: 20px">注册</el-link>
            </div>
        </el-form>
    </div>
<script>
    new Vue({
        el: '#app',
        data: {
            form: {
                username: '',
                password: ''
            },
            success: false
        },
        methods: {
            login() {
                //发送get请求并附带表单参数
                axios(Api.login(this.form)).then(e => {
                    this.success = true
                    setTimeout(function () {
                        window.location.href="/index.html"
                    },500)
                }).catch(e => {
                    this.$alert(e.response.data.message)
                })
            }
        }
    })
</script>
</body>

</html>